package com.blockchain.securewallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.model.SendRecord;
import com.blockchain.securewallet.utils.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SendRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;
    private Context context;

    private List<SendRecord> records = null;

    private IDataChange listener;
    private ArrayAdapter<String> adapter;

    private static final String[] btcUnit = {"BTC", "mBTC", "uBTC", "聪"};
    private static final String[] ethUnit = {"ETH", "Gwei"};
    //待补充
    private static final String[] ltcUnit = {"LTC"};
    private static final String[] bchUnit = {"BCH"};
    private static final String[] eosUnit = {"EOS"};


    public SendRecordAdapter(Context context, List<SendRecord> records) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.records = records;
    }

    public void setAddresses(List<SendRecord> records) {
        this.records = records;
    }

    public void setDataChange(IDataChange listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new SendRecordAdapter.SendRecordItem(mLayoutInflater.inflate(R.layout.send_record_item, parent, false));
        } else {
            return new SendRecordAdapter.AddRecordItem(mLayoutInflater.inflate(R.layout.item_add_record, parent, false));
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position < records.size()) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (position < records.size()) {
            final SendRecord record = records.get(position);
            String value = record.getValue();
            SendRecordItem sendRecordItem = (SendRecordItem) holder;
            sendRecordItem.editCount.setText(value);

            sendRecordItem.editAddress.setText(record.getAddress());
            if (position == 0) {
                sendRecordItem.editAddress.requestFocus();
            }

            int currentAddress = position + 1;
            sendRecordItem.tv_address_label.setText("地址" + currentAddress + ":");

            if (record.getType() == SendRecord.TYPE_BIT_COIN) {
                adapter = new ArrayAdapter<String>(
                        context, R.layout.spinner_stytle,
                        btcUnit);

            } else if (record.getType() == SendRecord.TYPE_ETH_COIN) {
                adapter = new ArrayAdapter<String>(
                        context, R.layout.spinner_stytle,
                        ethUnit);

            } else if (record.getType() == SendRecord.TYPE_LTC_COIN) {
                adapter = new ArrayAdapter<String>(
                        context, R.layout.spinner_stytle,
                        ltcUnit);

            } else if (record.getType() == SendRecord.TYPE_BCH_COIN) {
                adapter = new ArrayAdapter<String>(
                        context, R.layout.spinner_stytle,
                        bchUnit);

            } else if (record.getType() == SendRecord.TYPE_EOS_COIN) {
                adapter = new ArrayAdapter<String>(
                        context, R.layout.spinner_stytle,
                        eosUnit);

            }

            adapter.setDropDownViewResource(R.layout.spinner_dropdown_stytle);

            sendRecordItem.sp_unit.setAdapter(adapter);

            sendRecordItem.sp_unit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                    String str = arg0.getItemAtPosition(arg2).toString();
//                Toast.makeText(context, str, Toast.LENGTH_LONG).show();

                    if (record.getType() == SendRecord.TYPE_BIT_COIN) {
                        if (arg2 == 0) {
                            record.setUnit(0);
                        } else if (arg2 == 1) {
                            record.setUnit(3);
                        } else if (arg2 == 2) {
                            record.setUnit(6);
                        } else if (arg2 == 3) {
                            record.setUnit(8);
                        }

                    } else {
                        if (arg2 == 0) {
                            record.setUnit(0);
                        } else if (arg2 == 1) {
                            record.setUnit(9);
                        }
                    }

                    Logger.ds("Spinner onItemSelected被调用了: " + arg2);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });
        } else {

        }


    }

    @Override
    public int getItemCount() {
//        return records==null?0:records.size();
        return records.size() + 1;
    }

    class AddRecordItem extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_add_record)
        TextView tv_add_record;

        @BindView(R.id.ctl_add_record)
        ConstraintLayout ctl_add_record;

        public AddRecordItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            if (records.get(0).getType() == SendRecord.TYPE_BIT_COIN) {
                tv_add_record.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onAddRecord();
                        }
                    }
                });
            } else {
                ctl_add_record.setVisibility(View.GONE);
            }

        }
    }

    class SendRecordItem extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        @BindView(R.id.iv_address_book)
        ImageView ivAddressBook;

        @BindView(R.id.iv_qr_scan)
        ImageView ivScan;

        @BindView(R.id.edit_address)
        EditText editAddress;

        @BindView(R.id.edit_count)
        EditText editCount;

        @BindView(R.id.sp_unit)
        Spinner sp_unit;

        @BindView(R.id.tv_address_label)
        TextView tv_address_label;

        public SendRecordItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            editAddress.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (listener != null) {
                        listener.onAddressChange(getAdapterPosition(), s.toString());
                    }
                }
            });

            editCount.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (listener != null) {
                        listener.onValueChange(getAdapterPosition(), s.toString());
                    }
                }
            });
        }

        @OnClick(R.id.iv_delete)
        void delete() {
            if (listener != null)
                if (records.size() > 1) {
                    listener.onDelete(getAdapterPosition());
                } else {
                    editAddress.setText("");
                    editCount.setText("");
                }


        }

        @OnClick(R.id.iv_qr_scan)
        void scan() {
            if (listener != null) {
                listener.onScan(getAdapterPosition());
            }
        }

        @OnClick(R.id.iv_address_book)
        void open() {
            if (listener != null)
                listener.onOpenBook(getAdapterPosition());
        }
    }

    public interface IDataChange {
        void onScan(int pos);

        void onOpenBook(int pos);

        void onDelete(int pos);

        void onAddressChange(int pos, String address);

        void onValueChange(int pos, String value);

        void onUnitChange(int pos, int unit);

        void onAddRecord();
    }
}
